use std::{collections::HashMap, thread::sleep, time::Duration};

use container_application::{get_application_handler, SimpleApplicationHandler};
use container_core::workflow::{SimpleWorkflowEngine, WorkflowDocumentJsonReader};
use model_graph_types::{
    application::{
        agent::{AgentChatRequestMessage, AgentChatRequestMessageBuilder},
        ApplicationHandler,
    },
    container::workflow::{
        WorkflowDocumentReadeOptions, WorkflowDocumentReader, WorkflowEngine,
        WorkflowEngineCompileOptions, WorkflowEngineRunOptions, WorkflowGraphNode,
        WorkflowGraphNodeData, WorkflowGraphNodeHttpStatement, WorkflowGraphNodeStatement,
    },
    database::{ConnectorProtocol, DataSource as DatabaseDataSource, DatabaseType},
    modeling::{set_system_meta_database, Value},
};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::DEBUG)
        .init();
    tracing::info!("RRAI 启动...");

    set_system_meta_database(DatabaseDataSource {
        id: 0,
        database_type: DatabaseType::SQLite,
        protocol: ConnectorProtocol::SQLite,
        url: String::from("sqlite:/Users/jinisu/rrg01/rrg01.sqlite"),
        ..Default::default()
    })?;

    // let node = WorkflowGraphNode {
    //     id: String::from("...."),
    //     r#type: String::from("...."),
    //     data: WorkflowGraphNodeData::Statement(WorkflowGraphNodeStatement::Http(
    //         WorkflowGraphNodeHttpStatement {
    //             url: String::from("...."),
    //             method: None,
    //         },
    //     )),
    //     parent_id: None,
    // };
    // tracing::info!("{}", serde_json::to_string(&node)?);
    let application_identifier = String::from("test_app_001");

    let handler = get_application_handler(&application_identifier, false).await?;

    let request = AgentChatRequestMessageBuilder::default().build()?;

    let res = handler.lock().await.agent_chat_request(request).await;

    match res {
        Ok(response) => {
            tracing::debug!("1{:?}", response);
        }
        Err(err) => {
            tracing::error!("2{:?}", err);
        }
    }
    tokio::select! {
        _ = tokio::signal::ctrl_c() => {
            println!();
        }
    };
    tracing::info!("RRAI 结束...");
    Ok(())
}
